package cn.hu.basic.util;

import cn.hu.basic.jwt.JwtUtils;
import cn.hu.basic.jwt.Payload;
import cn.hu.basic.jwt.RsaUtils;
import cn.hu.basic.jwt.UserInfo;
import cn.hu.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class CurrentLoginInfoUtil {

    public static Logininfo currentLoginInfo(HttpServletRequest request) throws Exception {
        //从请求头中获取U-TOKEN
        String token = request.getHeader("U-TOKEN");
        if(token != null){
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
            Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
            return payload.getUserInfo().getLogininfo();
        }
        return null;
    }
}
